/*     */ package com.yc.importdata;
/*     */ 
/*     */ import com.yc.dao.importdata.ImportDAO;
/*     */ import com.yc.dao.model.FileBO;
/*     */ import com.yc.service.FolderService;
/*     */ import com.yc.upload.UploadServlet;
/*     */ import com.yc.util.ListContentsBFO;
/*     */ import com.yc.util.UtilBtsm;
/*     */ import com.yc.web.importdata.ImportUtil;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintWriter;
/*     */ import java.net.SocketException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Properties;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServlet;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.fileupload.FileItem;
/*     */ import org.apache.commons.fileupload.FileUploadException;
/*     */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*     */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*     */ import org.apache.commons.logging.Log;
/*     */ import org.apache.commons.logging.LogFactory;
/*     */ import org.apache.poi.hssf.usermodel.HSSFSheet;
/*     */ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/*     */ 
/*     */ public class ImportUploadServlet extends HttpServlet
/*     */ {
/*  42 */   private static final Log log = LogFactory.getLog(ImportUploadServlet.class);
/*     */ 
/*  44 */   private String propertiesFileName = "/btsm.properties";
/*  45 */   private String importFilePath = null;
/*  46 */   private ImportDAO dao = new ImportDAO();
/*     */ 
/*     */   public ImportUploadServlet()
/*     */   {
/*     */     try
/*     */     {
/*  54 */       init();
/*     */     }
/*     */     catch (ServletException e) {
/*  57 */       e.printStackTrace();
/*     */     }
/*  59 */     initImportFilePath();
/*  60 */     UtilBtsm.createFolder(this.importFilePath);
/*     */   }
/*     */ 
/*     */   public void destroy()
/*     */   {
/*  68 */     super.destroy();
/*     */   }
/*     */ 
/*     */   public void doGet(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/*     */   }
/*     */ 
/*     */   private synchronized void initImportFilePath()
/*     */   {
/*  89 */     if (this.importFilePath == null)
/*  90 */       this.importFilePath = UploadServlet.getImportUploadFilePath();
/*     */   }
/*     */ 
/*     */   public void doPost(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/* 105 */     log.info("开始上传文件");
/* 106 */     String strReturn = "{success:true,msg:'文件上传成功！'}";
/*     */     try
/*     */     {
/* 109 */       DiskFileItemFactory factory = new DiskFileItemFactory();
/* 110 */       ServletFileUpload sevletFileUpload = new ServletFileUpload(factory);
/* 111 */       initImportFilePath();
/* 112 */       UtilBtsm.createFolder(this.importFilePath);
/* 113 */       sevletFileUpload.setHeaderEncoding("utf-8");
/* 114 */       List fileItems = sevletFileUpload.parseRequest(request);
/* 115 */       Iterator iter = fileItems.iterator();
/* 116 */       String fileName = null;
/*     */ 
/* 118 */       iter = fileItems.iterator();
/* 119 */       while (iter.hasNext())
/*     */       {
/* 121 */         FileItem item = (FileItem)iter.next();
/* 122 */         fileName = item.getName();
/* 123 */         if (!item.isFormField())
/*     */         {
/* 125 */           log.info("上传文件名是：[" + fileName + "]");
/* 126 */           if (fileName.lastIndexOf("\\") != -1)
/* 127 */             fileName = fileName.substring(fileName.lastIndexOf("\\"));
/* 128 */           File fullFile = new File(fileName);
/* 129 */           File savedFile = new File(this.importFilePath, fullFile.getName());
/* 130 */           item.write(savedFile);
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (FileUploadException eup)
/*     */     {
/* 138 */       eup.printStackTrace();
/* 139 */       log.error(eup);
/*     */     }
/*     */     catch (SocketException clietEx) {
/* 142 */       log.warn("客户取消上传");
/*     */     }
/*     */     catch (Exception e) {
/* 145 */       strReturn = "{success:false,msg:'文件上传失败！'}";
/* 146 */       e.printStackTrace();
/*     */     }
/* 148 */     log.info(strReturn);
/* 149 */     response.setContentType("text/html; charset=utf-8");
/* 150 */     response.getWriter().print(strReturn);
/*     */   }
/*     */ 
/*     */   public void init()
/*     */     throws ServletException
/*     */   {
/*     */     try
/*     */     {
/* 162 */       Properties p = new Properties();
/* 163 */       InputStream in = FolderService.class.getResourceAsStream(this.propertiesFileName);
/* 164 */       p.load(in);
/* 165 */       in.close();
/*     */ 
/* 167 */       if (p.containsKey("importFilePath")) {
/* 168 */         this.importFilePath = p.getProperty("importFilePath");
/*     */       }
/* 170 */       log.info("importFilePath=" + this.importFilePath);
/*     */     }
/*     */     catch (Exception e) {
/* 173 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public ListContentsBFO getAllFiles(Map condition)
/*     */   {
/* 181 */     ListContentsBFO bfo = new ListContentsBFO();
/* 182 */     File d = new File(this.importFilePath);
/* 183 */     File[] fl = d.listFiles();
/* 184 */     List list = new ArrayList();
/* 185 */     for (int i = 0; i < fl.length; i++) {
/* 186 */       File e = fl[i];
/* 187 */       if (e.isFile()) {
/* 188 */         FileBO fileBo = new FileBO();
/* 189 */         fileBo.setId(String.valueOf(i));
/* 190 */         fileBo.setFileName(e.getName());
/* 191 */         list.add(fileBo);
/*     */       }
/*     */     }
/*     */ 
/* 195 */     bfo.setData(list.toArray());
/* 196 */     bfo.setTotalSize(0);
/* 197 */     return bfo;
/*     */   }
/*     */ 
/*     */   public boolean deleteFile(String strFileName)
/*     */   {
/* 202 */     boolean flag = true;
/*     */     try {
/* 204 */       UtilBtsm.deleteFile(this.importFilePath, strFileName);
/*     */     }
/*     */     catch (Exception e) {
/* 207 */       flag = false;
/*     */     }
/* 209 */     return flag;
/*     */   }
/*     */ 
/*     */   public boolean importDataToBase(String fileName) {
/* 213 */     File file = new File(this.importFilePath, fileName);
/* 214 */     log.info("import file: " + file.getAbsolutePath());
/*     */ 
/* 216 */     HSSFWorkbook workBook = null;
/*     */     try {
/* 218 */       workBook = new HSSFWorkbook(new FileInputStream(file));
/*     */     } catch (FileNotFoundException e) {
/* 220 */       e.printStackTrace();
/*     */     } catch (IOException e) {
/* 222 */       e.printStackTrace();
/*     */     }
/* 224 */     int sheetCount = workBook.getNumberOfSheets();
/* 225 */     log.info("sheetCount = [" + sheetCount + "]");
/*     */ 
/* 227 */     for (int i = 0; i < sheetCount; i++)
/*     */     {
/* 229 */       String arySheetNames = workBook.getSheetName(i);
/* 230 */       log.info("arySheetNames[" + i + "] = [" + arySheetNames + "]");
/*     */     }
/*     */ 
/* 233 */     HSSFSheet sheet = workBook.getSheet("无线设备");
/* 234 */     if (sheet != null)
/*     */     {
/* 236 */       log.info("导入【无线设备】！");
/* 237 */       List list = ImportUtil.importBtsNewData(sheet);
/* 238 */       this.dao.importBtsNewData(list);
/*     */     }
/*     */ 
/* 241 */     sheet = workBook.getSheet("交直流配电");
/* 242 */     if (sheet != null)
/*     */     {
/* 244 */       log.info("导入【交直流配电】！");
/* 245 */       List list = ImportUtil.importAcdcDistributionData(sheet);
/* 246 */       this.dao.importAcdcDistributionData(list);
/*     */     }
/*     */ 
/* 249 */     sheet = workBook.getSheet("塔放");
/* 250 */     if (sheet != null)
/*     */     {
/* 252 */       log.info("导入【塔放】！");
/* 253 */       List list = ImportUtil.importMagnifierData(sheet);
/* 254 */       this.dao.importMagnifierData(list);
/*     */     }
/*     */ 
/* 257 */     sheet = workBook.getSheet("电源柜");
/* 258 */     if (sheet != null)
/*     */     {
/* 260 */       log.info("导入【电源柜】！");
/* 261 */       List list = ImportUtil.imporPowerBoxData(sheet);
/* 262 */       this.dao.imporPowerBoxData(list);
/*     */     }
/*     */ 
/* 265 */     sheet = workBook.getSheet("蓄电池");
/* 266 */     if (sheet != null)
/*     */     {
/* 268 */       log.info("导入【蓄电池】！");
/* 269 */       List list = ImportUtil.imporStorageBatteryData(sheet);
/* 270 */       this.dao.imporStorageBatteryData(list);
/*     */     }
/*     */ 
/* 273 */     sheet = workBook.getSheet("空调");
/* 274 */     if (sheet != null)
/*     */     {
/* 276 */       log.info("导入【空调】！");
/* 277 */       List list = ImportUtil.imporAirconditionData(sheet);
/* 278 */       this.dao.imporAirconditionData(list);
/*     */     }
/*     */ 
/* 281 */     sheet = workBook.getSheet("灭火器");
/* 282 */     if (sheet != null)
/*     */     {
/* 284 */       log.info("导入【灭火器】！");
/* 285 */       List list = ImportUtil.importFireExtinGuisherData(sheet);
/* 286 */       this.dao.importFireExtinGuisherData(list);
/*     */     }
/*     */ 
/* 289 */     sheet = workBook.getSheet("B级防雷器");
/* 290 */     if (sheet != null)
/*     */     {
/* 292 */       log.info("导入【B级防雷器】！");
/* 293 */       List list = ImportUtil.importBathunderData(sheet);
/* 294 */       this.dao.importBathunderData(list);
/*     */     }
/*     */ 
/* 297 */     sheet = workBook.getSheet("动环监控");
/* 298 */     if (sheet != null)
/*     */     {
/* 300 */       log.info("导入【动环监控】！");
/* 301 */       List list = ImportUtil.importEnvMontorData(sheet);
/* 302 */       this.dao.importEnvMontorData(list);
/*     */     }
/*     */ 
/* 305 */     sheet = workBook.getSheet("发电机组");
/* 306 */     if (sheet != null)
/*     */     {
/* 308 */       log.info("导入【发电机组】！");
/* 309 */       List list = ImportUtil.importDynamoData(sheet);
/* 310 */       this.dao.importDynamoData(list);
/*     */     }
/*     */ 
/* 314 */     sheet = workBook.getSheet("变压器");
/* 315 */     if (sheet != null)
/*     */     {
/* 317 */       log.info("导入【变压器】！");
/* 318 */       List list = ImportUtil.importTransformerData(sheet);
/* 319 */       this.dao.importTransformerData(list);
/*     */     }
/*     */ 
/* 322 */     sheet = workBook.getSheet("传输");
/* 323 */     if (sheet != null)
/*     */     {
/* 325 */       log.info("导入【传输】！");
/* 326 */       List list = ImportUtil.importForTransfersData(sheet);
/* 327 */       this.dao.importForTransfersData(list);
/*     */     }
/*     */ 
/* 330 */     sheet = workBook.getSheet("市电引入");
/* 331 */     if (sheet != null)
/*     */     {
/* 333 */       log.info("导入【市电引入】！");
/* 334 */       List list = ImportUtil.importForMainssupplyData(sheet);
/* 335 */       this.dao.importForMainssupplyData(list);
/*     */     }
/*     */ 
/* 338 */     sheet = workBook.getSheet("天馈");
/* 339 */     if (sheet != null)
/*     */     {
/* 341 */       log.info("导入【天馈】！");
/* 342 */       List list = ImportUtil.importForAntennaFeederData(sheet);
/* 343 */       this.dao.importForAntennaFeederData(list);
/*     */     }
/*     */ 
/* 346 */     sheet = workBook.getSheet("铁塔");
/* 347 */     if (sheet != null)
/*     */     {
/* 349 */       log.info("导入【铁塔】！");
/* 350 */       List list = ImportUtil.importForIronTowerData(sheet);
/* 351 */       this.dao.importForIronTowerData(list);
/*     */     }
/*     */ 
/* 354 */     sheet = workBook.getSheet("机房");
/* 355 */     if (sheet != null)
/*     */     {
/* 357 */       log.info("导入【机房】！");
/* 358 */       List list = ImportUtil.importForMachineRoomData(sheet);
/* 359 */       this.dao.importForMachineRoomData(list);
/*     */     }
/*     */ 
/* 362 */     sheet = workBook.getSheet("基础供电归属");
/* 363 */     if (sheet != null)
/*     */     {
/* 365 */       log.info("导入【基础供电归属】！");
/* 366 */       List list = ImportUtil.importForPowerSupplyData(sheet);
/* 367 */       this.dao.importForPowerSupplyData(list);
/*     */     }
/*     */ 
/* 370 */     sheet = workBook.getSheet("交通状况");
/* 371 */     if (sheet != null)
/*     */     {
/* 373 */       log.info("导入【交通状况】！");
/* 374 */       List list = ImportUtil.importForTrafficInfoData(sheet);
/* 375 */       this.dao.importForTrafficInfoData(list);
/*     */     }
/*     */ 
/* 378 */     sheet = workBook.getSheet("地理环境");
/* 379 */     if (sheet != null)
/*     */     {
/* 381 */       log.info("导入【地理环境】！");
/* 382 */       List list = ImportUtil.importForEnvData(sheet);
/* 383 */       this.dao.importForEnvData(list);
/*     */     }
/*     */ 
/* 386 */     return true;
/*     */   }
/*     */ }

/* Location:           E:\java\apache-tomcat-6.0.36\webapps\btsm\WEB-INF\classes\
 * Qualified Name:     com.yc.importdata.ImportUploadServlet
 * JD-Core Version:    0.6.2
 */